package com.pingan.agentbank.aaos.util;

import com.pingan.agentbank.aaos.exception.AAOSException;
import com.pingan.vframe.logging.Log;
import com.pingan.vframe.logging.LogFactory;
import com.pingan.vframe.sdo.ServiceData;

import java.io.UnsupportedEncodingException;

public class FixedLenUtil {

    private static final Log log = LogFactory.getLog();

    /*
    * 转换字符串，不足右补空；为空直接补空
    * fileName：文件名  count：行号  field：通联定长的key   maxLength：长度  code：目标编码
    */
    public static String convertStr(String approMsg,String fileName,int count,String field, ServiceData serviceData, int maxLength, String code){

        String str = serviceData.getString(field);

        if (isBlank(str)){
            return supplyBlank(maxLength);
        } else {
            StringBuffer sb = new StringBuffer(str);
            int length = 0;
            try {
                length = str.getBytes(code).length;
            } catch (UnsupportedEncodingException e) {
                log.error("[ AAOS ] "+fileName + " row " + count+ field+"unsupported encoding",e);
                throw new AAOSException("[ AAOS ] "+fileName + " row " + count+ field+"unsupported encoding",e);
            }

            if (length>maxLength){
                //抛出异常
                log.error("[ AAOS ] "+"FieldOverLengthError"+" (BusinessModule) "+fileName + " row " + count +" "+ field +"{"+serviceData.getString(field)+"}"+ " over length " + approMsg);
                throw new AAOSException("[ AAOS ] "+"FieldOverLengthError"+" (BusinessModule) "+fileName + " row " + count + " " + field + "{"+serviceData.getString(field)+"}"+ " over length " + approMsg);
            } else if (length == maxLength){
                return str;
            } else {
                for (int j=0;j<maxLength-length;j++){
                    sb.append(" ");
                }
                return sb.toString();
            }
        }
    }

    /*
    * 转换数字,不足左补0；为空直接补空
    * fileName：文件名  count：行号  field：通联定长的key   maxLength：长度  code：目标编码
    */
    public static String convertNum(String approMsg,String fileName,int count,String field,ServiceData serviceData,int maxLength,String code){


        String str = serviceData.getString(field);
        String supply = "0";

        if (isBlank(str)){
            return supplyBlank(maxLength);
        } else {
            Integer.valueOf(str);

            int length = 0;
            try {
                length = str.getBytes(code).length;
            } catch (UnsupportedEncodingException e) {
                log.error("[ AAOS ] "+fileName + " row " + count + field + "unsupported encoding",e);
                throw new AAOSException("[ AAOS ] "+fileName + " row " + count + field + "unsupported encoding",e);
            }

            if (length>maxLength){
                //抛出异常
                log.error("[ AAOS ] "+"FieldOverLengthError"+" (BusinessModule) "+fileName + " row " + count +" "+ field + "{"+serviceData.getString(field)+"}"+" over length");
                throw new AAOSException("[ AAOS ] "+"FieldOverLengthError"+" (BusinessModule) "+fileName + " row " + count +" "+ field + "{"+serviceData.getString(field)+"}"+" over length");
            } else if (length == maxLength){
                return str;
            } else {
                StringBuffer sb = new StringBuffer();
                for (int j = 0;j < maxLength-length;j++){
                    sb.append(supply);
                }
                return sb.append(str).toString();
            }
        }
    }

    /*
    * 如果为空，则补空格
    */
    public static String supplyBlank(int n){

        StringBuffer sb = new StringBuffer();
        for (int i = 0;i < n;i++){
            sb.append(" ");
        }
        return sb.toString();
    }

    /*
    * 是否为空判断，null或者空字符串都算空
    */
    public static boolean isBlank(String str){

        if (str == null){
            return true;
        }

        if (str.equals("")){
            return true;
        }
        return false;
    }
}
